<template>
	<view class="page_scroll_view">
		<tn-navbar fixed :bottom-shadow="false">商家配置</tn-navbar>
		<loading v-if="isLoading" style="height: 70vh;"></loading>
		<view class="empty" v-else-if="!sellerConfigInfo">
			<image
				src="https://sgymv1.oss-cn-beijing.aliyuncs.com/sgym/avatar/admin/2025/02/a0c4966b-11af-4c87-93aa-359f9c03d89f83ee61eba8e363790692eb5fd8a98de7.png"
				mode="widthFix"></image>
			<text>这里空空荡荡，啥也没有留下</text>
		</view>
		<scroll-view scroll-y="true" v-else>
			<view class="tn-flex tn-flex-column tn-flex-center tn-p-sm">
				<tn-form :model="sellerConfigInfo" ref="formRef" :rules="formRules" label-position="top">
					<tn-form-item label="货券增发比例" prop="seller_raise_rate">
						<tn-input type="number" v-model="sellerConfigInfo.seller_raise_rate" placeholder="请输入货券增发比例"
							clearable />
					</tn-form-item>
					<tn-form-item label="申请增发默认最大值" prop="seller_raise_max">
						<tn-input type="number" v-model="sellerConfigInfo.seller_raise_max" placeholder="请输入申请增发默认最大值"
							clearable />
					</tn-form-item>
					<tn-form-item label="增发审核开关">
						<tn-switch v-model="sellerConfigInfo.seller_raise_switch" shape="square" :active-value="1"
							:inactive-value="0" active-text="开启" active-color="tn-green" inactive-text="关闭"
							@change="submitForReview" />
					</tn-form-item>
					<tn-form-item label="默认回券比例" prop="seller_back_rate">
						<tn-input type="number" v-model="sellerConfigInfo.seller_back_rate" placeholder="请输入默认回券比例"
							clearable />
					</tn-form-item>
					<tn-form-item label="默认平台比例" prop="seller_plat_rate">
						<tn-input type="number" v-model="sellerConfigInfo.seller_plat_rate" placeholder="请输入默认平台比例"
							clearable />
					</tn-form-item>
					<tn-form-item label="默认扫码比例" prop="seller_alone_rate">
						<tn-input type="number" v-model="sellerConfigInfo.seller_alone_rate" placeholder="请输入默认扫码比例"
							clearable />
					</tn-form-item>
					<tn-form-item label="默认销售比例" prop="seller_sale_rate">
						<tn-input type="number" v-model="sellerConfigInfo.seller_sale_rate" placeholder="请输入默认销售比例"
							clearable />
					</tn-form-item>
					<tn-form-item label="默认套餐券比例" prop="seller_combo_rate">
						<tn-input type="number" v-model="sellerConfigInfo.seller_combo_rate" placeholder="请输入默认套餐券比例"
							clearable />
					</tn-form-item>
					<tn-form-item label="默认销售套餐比例" prop="seller_combo_sale_rate">
						<tn-input type="number" v-model="sellerConfigInfo.seller_combo_sale_rate"
							placeholder="请输入默认销售套餐比例" clearable />
					</tn-form-item>
				</tn-form>
				<view class="tn-flex-center">
					<tn-button size="lg" width="80vw" height="80" font-size="26"
						@click="submitForReview">确定修改</tn-button>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
	import {
		ref,
	} from 'vue'
	import {
		getSellerConfigApi
	} from "@/request/worktop/system.js"
	import {
		onLoad
	} from "@dcloudio/uni-app"

	onLoad(() => {
		getSellerConfig()
	})

	const isLoading = ref(true)
	const sellerConfigInfo = ref({
		seller_raise_rate: 0,
		seller_raise_max: 0,
		seller_raise_switch: 0,
		seller_back_rate: 0,
		seller_plat_rate: 0,
		seller_alone_rate: 0,
		seller_sale_rate: 0,
		seller_combo_rate: 0,
		seller_combo_sale_rate: 0
	})
	// 规则
	const formRules = {
		seller_raise_rate: [{
			required: true,
			message: '请输入货券增发比例',
			trigger: ['change', 'blur']
		}, {
			pattern: /^\d{1,4}$/,
			message: '请输入0-9999之间的数字',
			trigger: ['change', 'blur'],
		}],
		seller_raise_max: [{
			required: true,
			message: '请输入申请增发默认最大值',
			trigger: ['change', 'blur']
		}, {
			pattern: /^\d{1,4}$/,
			message: '请输入0-9999之间的数字',
			trigger: ['change', 'blur'],
		}],
		seller_back_rate: [{
			required: true,
			message: '请输入默认回券比例',
			trigger: ['change', 'blur']
		}, {
			pattern: /^\d{1,4}$/,
			message: '请输入0-9999之间的数字',
			trigger: ['change', 'blur'],
		}],
		seller_plat_rate: [{
			required: true,
			message: '请输入默认平台比例',
			trigger: ['change', 'blur']
		}, {
			pattern: /^\d{1,4}$/,
			message: '请输入0-9999之间的数字',
			trigger: ['change', 'blur'],
		}],
		seller_alone_rate: [{
			required: true,
			message: '请输入默认扫码比例',
			trigger: ['change', 'blur']
		}, {
			pattern: /^\d{1,4}$/,
			message: '请输入0-9999之间的数字',
			trigger: ['change', 'blur'],
		}],
		seller_sale_rate: [{
			required: true,
			message: '请输入默认销售比例',
			trigger: ['change', 'blur']
		}, {
			pattern: /^\d{1,4}$/,
			message: '请输入0-9999之间的数字',
			trigger: ['change', 'blur'],
		}],
		seller_combo_rate: [{
			required: true,
			message: '请输入默认套餐券比例',
			trigger: ['change', 'blur']
		}, {
			pattern: /^\d{1,4}$/,
			message: '请输入0-9999之间的数字',
			trigger: ['change', 'blur'],
		}],
		seller_combo_sale_rate: [{
			required: true,
			message: '请输入默认销售套餐比例',
			trigger: ['change', 'blur']
		}, {
			pattern: /^\d{1,4}$/,
			message: '请输入0-9999之间的数字',
			trigger: ['change', 'blur'],
		}],
	}

	// 获取商家配置详情
	const getSellerConfig = (type = '') => {
		getSellerConfigApi(sellerConfigInfo.value, type).then(res => {
			if (res.code == 0) {
				sellerConfigInfo.value = res.data
				sellerConfigInfo.value.seller_raise_switch = Number(sellerConfigInfo.value.seller_raise_switch)
			}
		}).catch(err => {
			uni.showToast({
				title: err.message,
				icon: 'none'
			})
		}).finally(() => {
			setTimeout(() => {
				isLoading.value = false
			}, 500)
		})
	}
	// 确定修改商家配置
	const submitForReview = () => {
		getSellerConfigApi(sellerConfigInfo.value, 'edit').then(res => {
			if (res.code == 0) {
				uni.showToast({
					title: res.message
				})
			}
		}).catch(err => {
			uni.showToast({
				title: err.message,
				icon: 'none'
			})
		})
	}
</script>

<style scoped lang="scss">
</style>